﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class UISForm : Form
    {
        public UISForm()
        {
            InitializeComponent();
        }

        bool insert = true;
        tb_UI uis;

        private void UISForm_Load(object sender, EventArgs e)
        {
            FillTable();
        }

        private void FillTable()
        {
            dgvUIS.DataSource = DBClass.RESPContext.tb_UIs.Where(wh => wh.ID > 0);
            dgvUIS.Columns["Name"].HeaderText = InformationClass.Lang ? "УИС" : "ИТБ";
            dgvUIS.Columns["ID"].Visible = false;
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = insert = true;
            buttonAdd.Text = InformationClass.Lang ? "Добавить" : "Қўшиш";
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvUIS.CurrentRow != null && dgvUIS.RowCount > 0)
            {
                groupBox1.Enabled = true;
                insert = false;
                buttonAdd.Text = InformationClass.Lang ? "Редак." : "Таҳрир қилиш";

                uis = DBClass.UIS.Where(wh => wh.ID == (int)dgvUIS["ID", dgvUIS.CurrentRow.Index].Value).SingleOrDefault();
                tbName.Text = uis.Name;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbName.Text))
            {
                if (insert)
                {
                    if (DBClass.UIS.Where(wh => wh.Name == tbName.Text).Select(se => se.Name).ToArray().Contains(tbName.Text))
                    {
                        MessageBox.Show(msgClass.msgUncorrectData, msgClass.msgAchtung);
                        return;
                    }

                    uis = new tb_UI
                    {
                        Name = tbName.Text
                    };
                    DBClass.UIS.InsertOnSubmit(uis);
                }
                else
                {
                    uis.Name = tbName.Text;
                }
                try
                {
                    if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                    {
                        DBClass.RESPContext.SubmitChanges();
                        MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                        FillTable();
                    }
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
                groupBox1.Enabled = false;
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvUIS.CurrentRow != null && dgvUIS.RowCount > 0)
            {
                uis = DBClass.UIS.Where(wh => wh.ID == (int)dgvUIS["ID", dgvUIS.CurrentRow.Index].Value).SingleOrDefault();
                if (uis != null)
                {
                    string msg = InformationClass.Lang ? "Удалить " + uis.Name + "?" : "Олиб ташлаш " + uis.Name + "?";
                    if (MessageBox.Show(msg, msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        DBClass.UIS.DeleteOnSubmit(uis);
                        DBClass.RESPContext.SubmitChanges();
                        FillTable();
                    }
                }
            }
        }

    }
}
